package cn._2dland.ui
{
	import cn._2dland.events.ResizeEvent;
	
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	[Event(name="resize",type="cn._2dland.events.ResizeEvent")]

	/**
	 * 可调整大小的组件
	 * @author deadblue
	 * 
	 */
	public class ResizableComponect extends Sprite
	{
		protected var _width:Number = 0;
		protected var _height:Number = 0;

		protected var _backgroundColor:Number = 0xffffff;
		protected var _backgroundAlpha:Number = 1;

		public function ResizableComponect() {
			super();
		}

		override public function set width(value:Number):void {
			_width = value;
			resize();
		}
		override public function get width():Number {
			return _width;
		}

		override public function set height(value:Number):void {
			_height = value;
			resize();
		}
		override public function get height():Number {
			return _height;
		}

		/**
		 * 设置组件尺寸
		 * @param width - 宽度
		 * @param height - 高度
		 * 
		 */		
		public function setSize(width:Number, height:Number):void {
			_width = width;
			_height = height;
			resize();
		}

		public function get backgroundColor():Number {
			return _backgroundColor;
		}
		public function set backgroundColor(value:Number):void {
			_backgroundColor = value;
			redraw();
		}

		public function get backgroundAlpha():Number {
			return _backgroundAlpha;
		}
		public function set backgroundAlpha(value:Number):void {
			_backgroundAlpha = value;
			redraw();
		}

		public function move(x:Number, y:Number):void {
			this.x = x;
			this.y = y;
		} 

		/**
		 * 重绘组件背景
		 */		
		protected function redraw():void {
			if(_width == 0 || _height == 0) return;

			graphics.beginFill(_backgroundColor, _backgroundAlpha);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
		}

		/**
		 * 调整组件大小，会调用重绘代码，并调度resize事件。
		 */		
		protected function resize():void {
			var oldWidth:Number = width;
			var oldHeight:Number = height;

			redraw();
			scrollRect = new Rectangle(0, 0, _width, _height);
			dispatchEvent(new ResizeEvent(ResizeEvent.RESIZE, oldWidth, oldHeight));
		}
	}
}